package com.ruoyi.movie.service.impl;

import java.util.List;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.movie.mapper.MovieBasicToCountryMapper;
import com.ruoyi.movie.domain.MovieBasicToCountry;
import com.ruoyi.movie.service.IMovieBasicToCountryService;

/**
 * 影视国家关联Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-28
 */
@Service
public class MovieBasicToCountryServiceImpl implements IMovieBasicToCountryService
{
    @Autowired
    private MovieBasicToCountryMapper movieBasicToCountryMapper;

    /**
     * 查询影视国家关联
     *
     * @param movieBasicToCountryId 影视国家关联主键
     * @return 影视国家关联
     */
    @Override
    public MovieBasicToCountry selectMovieBasicToCountryById(Integer movieBasicToCountryId)
    {
        return movieBasicToCountryMapper.selectMovieBasicToCountryById(movieBasicToCountryId);
    }

    /**
     * 查询影视国家关联列表
     *
     * @param movieBasicToCountry 影视国家关联
     * @return 影视国家关联
     */
    @Override
    public List<MovieBasicToCountry> selectMovieBasicToCountryList(MovieBasicToCountry movieBasicToCountry)
    {
        return movieBasicToCountryMapper.selectMovieBasicToCountryList(movieBasicToCountry);
    }

    /**
     * 根据影视ID查询关联的国家ID列表
     *
     * @param movieBasicsId 影视基础ID
     * @return 国家ID集合
     */
    @Override
    public List<Integer> selectCountryIdsByMovieBasicsId(Integer movieBasicsId)
    {
        return movieBasicToCountryMapper.selectCountryIdsByMovieBasicsId(movieBasicsId);
    }

    /**
     * 新增影视国家关联
     *
     * @param movieBasicToCountry 影视国家关联
     * @return 结果
     */
    @Override
    public int insertMovieBasicToCountry(MovieBasicToCountry movieBasicToCountry)
    {
        return movieBasicToCountryMapper.insertMovieBasicToCountry(movieBasicToCountry);
    }

    /**
     * 批量新增影视国家关联
     *
     * @param movieBasicsId 影视基础ID
     * @param countryIds 国家ID数组
     * @return 结果
     */
    @Override
    public int batchInsertMovieBasicToCountry(Integer movieBasicsId, Integer[] countryIds)
    {
        if (countryIds == null || countryIds.length == 0)
        {
            return 0;
        }

        List<MovieBasicToCountry> list = new ArrayList<>();
        for (Integer countryId : countryIds)
        {
            MovieBasicToCountry relation = new MovieBasicToCountry();
            relation.setMovieBasicsId(movieBasicsId);
            relation.setCountryId(countryId);
            list.add(relation);
        }

        return movieBasicToCountryMapper.batchInsertMovieBasicToCountry(list);
    }

    /**
     * 修改影视国家关联
     *
     * @param movieBasicToCountry 影视国家关联
     * @return 结果
     */
    @Override
    public int updateMovieBasicToCountry(MovieBasicToCountry movieBasicToCountry)
    {
        return movieBasicToCountryMapper.updateMovieBasicToCountry(movieBasicToCountry);
    }

    /**
     * 批量删除影视国家关联
     *
     * @param movieBasicToCountryIds 需要删除的影视国家关联主键
     * @return 结果
     */
    @Override
    public int deleteMovieBasicToCountryByIds(Integer[] movieBasicToCountryIds)
    {
        return movieBasicToCountryMapper.deleteMovieBasicToCountryByIds(movieBasicToCountryIds);
    }

    /**
     * 删除影视国家关联信息
     *
     * @param movieBasicToCountryId 影视国家关联主键
     * @return 结果
     */
    @Override
    public int deleteMovieBasicToCountryById(Integer movieBasicToCountryId)
    {
        return movieBasicToCountryMapper.deleteMovieBasicToCountryById(movieBasicToCountryId);
    }

    /**
     * 根据影视ID删除关联
     *
     * @param movieBasicsId 影视基础ID
     * @return 结果
     */
    @Override
    public int deleteMovieBasicToCountryByMovieBasicsId(Integer movieBasicsId)
    {
        return movieBasicToCountryMapper.deleteMovieBasicToCountryByMovieBasicsId(movieBasicsId);
    }

    /**
     * 保存影视国家关联（先删除旧的，再批量插入新的）
     *
     * @param movieBasicsId 影视基础ID
     * @param countryIds 国家ID数组
     * @return 结果
     */
    @Override
    public int saveMovieBasicToCountry(Integer movieBasicsId, Integer[] countryIds)
    {
        // 先删除旧的关联
        deleteMovieBasicToCountryByMovieBasicsId(movieBasicsId);

        // 再批量插入新的关联
        return batchInsertMovieBasicToCountry(movieBasicsId, countryIds);
    }
}
